#!/bin/bash

# Check for program updates.

# Copyright (C) 2003-2006 IBM
# 
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; either version 2 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
# 02111-1307, USA.


source "$POUNDER_HOME/libpounder.sh"

if [ ! -f "$POUNDER_HOME/README" ]; then
	echo "README file (with version number) is missing.  Not checking version."
	exit 0
fi

if [ -z "$POUNDER_HEAD" ]; then
	echo "No POUNDER_HEAD defined; can't check version."
	exit 0
fi

X=$(( `echo $POUNDER_VERSION | cut -c 1-4 | sed -e 's/^0*//g'` * 10000 + `echo $POUNDER_VERSION | cut -c 6-7 | sed -e 's/^0*//g'` * 100 + `echo $POUNDER_VERSION | cut -c 9-10 | sed -e 's/^0*//g'`))

LATEST_POUNDER=`wget -O - "$POUNDER_HEAD/.version" 2> /dev/null`
if [ -z "$LATEST_POUNDER" ]; then
	# If we couldn't get the version, then the variable is zero-length.
	echo "Unable to determine if there's a new pounder2 release."
	echo "This is not a fatal error, merely a warning."
	exit 0
fi

Y=$(( `echo $LATEST_POUNDER | cut -c 1-4 | sed -e 's/^0*//g'` * 10000 + `echo $LATEST_POUNDER | cut -c 6-7 | sed -e 's/^0*//g'` * 100 + `echo $LATEST_POUNDER | cut -c 9-10 | sed -e 's/^0*//g'`))

if [ $Y -gt $X ]; then
	echo "Version mismatch detected."
	echo "You are running: $POUNDER_VERSION."
	echo "The server has $LATEST_POUNDER."
	echo
	while true; do
		echo -en "Would you like to download it [Y/n]? "
		read f
		if [ "$f" == "Y" -o "$f" == "y" -o -z "$f" ]; then
			cd "$POUNDER_HOME/../"
			wget "$POUNDER_HEAD/pounder21-$LATEST_POUNDER.src.tar.gz"
			cd -
			echo "Please unpack your new pounder and rerun Install."
			exit 1
		elif [ "$f" == "N" -o "$f" == "n" ]; then
			echo "Ok; ignoring mismatch and proceeding."
			exit 0
		fi
	done
fi

exit 0
